package top.kaoshanji.p1ch02a;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * author kaoshanji
 * ^_^
 * create time 2025/2/10 9:59
 */
public class FilteringApples {

    private static final Logger logger = LogManager.getLogger(FilteringApples.class);

    public static void main(String[] args) {
        List<Apple> inventory = Arrays.asList(
                new Apple(80, Color.GREEN),
                new Apple(155, Color.GREEN),
                new Apple(120, Color.RED));

        // 代码清单-p1ch02a-24页a：把颜色作为参数
        List<Apple> greenApples = filterApplesByColor(inventory, Color.GREEN);
        List<Apple> redApples = filterApplesByColor(inventory, Color.RED);
        logger.info("...筛选绿色的苹果：{}...", greenApples);
        logger.info("...筛选红色的苹果：{}...", redApples);


        // 代码清单-p1ch02a-25页b：仅仅选出重的苹果
        List<Apple> heavyApples = filter(inventory, new AppleWeightPredicate());
        logger.info("...仅仅选出重的苹果：{}...", heavyApples);

        // 代码清单-p1ch02a-25页c：仅仅选出绿苹果
        List<Apple> greenApples2 = filter(inventory, new AppleColorPredicate());
        logger.info("...仅仅选出绿苹果：{}...", greenApples2);

        // 代码清单-p1ch02a-27页a：应对任何涉及苹果属性的需求变更
        List<Apple> redAndHeavyApples = filter(inventory, new AppleRedAndHeavyPredicate());
        logger.info("...应对任何涉及苹果属性的需求变更：{}...", redAndHeavyApples);


        // 代码清单-p1ch02a-31页a：第五次尝试：使用匿名类
        List<Apple> redApples2 = filter(inventory, new ApplePredicate() {
            @Override
            public boolean test(Apple apple) {
                return apple.getColor() == Color.RED;
            }
        });
        logger.info("...第五次尝试：使用匿名类：{}...", redApples2);

    }

    // 代码清单-p1ch02a-26页a：根据抽象条件筛选
    public static List<Apple> filter(List<Apple> inventory, ApplePredicate p) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple: inventory) {
            // 具体的逻辑被抽象了
            if (p.test(apple)) {
                result.add(apple);
            }
        }

        return result;
    }

    // 代码清单-p1ch02a-23页a：第一个解决方案-筛选绿苹果
    public static List<Apple> filterGreenApples(List<Apple> inventory) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple: inventory) {
            if (apple.getColor() == Color.GREEN) {
                result.add(apple);
            }
        }
        return result;
    }

    // 通过颜色筛选苹果
    public static List<Apple> filterApplesByColor(List<Apple> inventory, Color color) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple: inventory) {
            if (apple.getColor() == color) {
                result.add(apple);
            }
        }
        return result;
    }

    // 代码清单-p1ch02a-24页b：把重量作为参数
    public static List<Apple> filterApplesByWeight(List<Apple> inventory, int weight) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple: inventory) {
            if (apple.getWeight() > weight) {
                result.add(apple);
            }
        }
        return result;
    }



}
